package com.epoch.servicecourse.service.impl;

import bean.response.ResponseBean;
import bean.response.ResultEnum;
import com.epoch.servicecourse.bean.Course;
import com.epoch.servicecourse.dao.CourseDao;
import com.epoch.servicecourse.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.ResultInfoUtil;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 课程相关信息获取
 * @author Seven
 * @date 2021/11/16
 */
@Service
@Slf4j
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    /**
     * 获取指定课程编号的课程的信息
     * @param courseId
     * @return
     */
    @Override
    public Course getCourseById(String courseId) {
        return courseDao.getCourseById(courseId);
    }

    /**
     * 通过课程类型获得指定数量的课程信息
     * @param map
     * @return
     */
    @Override
    public List<Course> getCourseByTypeId(HashMap<String,String> map) {
        String typeId = map.get("typeId");
        Integer courseNum = Integer.parseInt(map.get("courseNum"));
        System.out.println(typeId);
        System.out.println(courseNum);
        List allCourse = courseDao.getCourseByTypeId(typeId);
        System.out.println(allCourse.size());
        List<Course> courseList = new ArrayList<>(courseNum);
        if (allCourse.size() < courseNum){
            return allCourse;
        }else{
            for (int i = 0; i < courseNum; i++) {
                courseList.add((Course) allCourse.get(i));
            }
        }
        return courseList;
    }

    @Override
    public ResponseBean getCourse() {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setCode(200);
        responseBean.setMsg("获取课程成功");
        responseBean.setData(courseDao.getCourse());
        return responseBean;
    }

    @Override
    public ResponseBean getPhoto(String courseId, HttpServletResponse response) {
        byte[] byteImage = courseDao.getCourseById(courseId).getPhoto();
        String photoType = courseDao.getCourseById(courseId).getPhotoType();
        if (byteImage != null){
            InputStream inputStream = new ByteArrayInputStream(byteImage);
            try {
                BufferedImage image = ImageIO.read(inputStream);
                if (Objects.equals(photoType, "jpg")){
                    ImageIO.write(image,photoType,response.getOutputStream());
                }else {
                    ImageIO.write(image,"png",response.getOutputStream());
                }
                return ResultInfoUtil.buildSuccess(ResultEnum.OK,"获取封面成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResultInfoUtil.buildSuccess(ResultEnum.BAD_REQUEST,"获取封面失败");
    }
}
